﻿using demo.Interfaces;
using demo.Models.DTOs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace demo.Controllers
{
    [ApiController]
    [Route("api/products")]
    public class ProductController : ControllerBase
    {
        private readonly IProductService _productService;

        public ProductController(IProductService productService)
        {
            _productService = productService;
        }

        //[HttpGet]
        //public async Task<IActionResult> GetProducts(int page, int pageSize)
        //{
        //    var allProducts = _productService.GetAllProductAsync();
        //    var totalCount = allProducts.Count;
        //    var products = await _productService.GetProductsAsync(page, 9);
        //    return Ok(products);
        //}
        [HttpGet]
        public async Task<IActionResult> GetProducts(int page = 1)
        {
            // 获取当前页的数据
            var products = await _productService.GetProductsAsync(page, 9);

            // 获取商品总数
            var totalCount = await _productService.GetAllProductsCountAsync();

            // 构建返回结果
            var result = new
            {
                items = products, // 当前页的数据
                totalCount = totalCount // 商品总数
            };

            return Ok(result); // 返回 JSON 数据
        }

        [HttpGet("search")]
        public async Task<IActionResult> SearchProduct(int? id, string name)
        {
            var product = await _productService.SearchProductAsync(id ?? 0, name);
            if (product == null) return NotFound();
            return Ok(product);
        }

        [HttpGet("filter")]
        public async Task<IActionResult> FilterProducts(
            [FromQuery] int? categoryId,
            [FromQuery] int? subCategoryId,
            [FromQuery] decimal? minPurchasePrice,
            [FromQuery] decimal? maxPurchasePrice)
        {
            try
            {
                // 调用服务层方法
                var products = await _productService.FilterProductsAsync(categoryId, subCategoryId, minPurchasePrice, maxPurchasePrice);

                // 返回结果
                return Ok(products);
            }
            catch (Exception ex)
            {
                // 记录异常并返回 500 错误
                Console.WriteLine($"Error: {ex.Message}");
                return StatusCode(500, new { message = "An error occurred while filtering products.", details = ex.Message });
            }
        }

        [HttpPost]
        public async Task<IActionResult> AddProduct([FromBody] AddProductDto productDto)
        {
            await _productService.AddProductAsync(productDto);
            return Created("", null);
        }

        [HttpPut]
        public async Task<IActionResult> UpdateProduct(UpdateProductDto productDto)
        {
            await _productService.UpdateProductAsync(productDto);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteProduct(int id)
        {
            await _productService.DeleteProductAsync(id);
            return NoContent();
        }
    }
}
